package com.iwhalecloud.bss.kite.cucc.order.flow.handles;

import com.iwhalecloud.bss.kite.common.constant.OrderFlowConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.order.flow.FlowEngine;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItem;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.Flow;
import com.iwhalecloud.bss.kite.dataservice.entity.flow.WqFinish;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.dataservice.mapper.flow.WqFinishMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.order.OrderItemMapper;
import com.ztesoft.bss.base.staticdata.CoreDataInteract;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.bss.order.base.consts.KeyValues;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 新竣工环节处理（20100504）。 <br>
 * 该处理器完成如下的功能：在条件满足的情况下向竣工派单。
 */
public class CompInstCompleteHandler extends DefaultTacheHandler {

    public String execute(Flow flow) {

        // 获取订单项
        OrderItem orderItem = getOrderItem(flow);
        if (Objects.isNull(orderItem)) {
            return flow.getTacheId();
        }

        // 流程睡眠中或者 竣工自动派生工单不处理
        if (KiteStringUtils.isEqual(OrderFlowConsts.FLOW_STATUS_SLEEPED, flow.getStatus())
            || KiteStringUtils.isEqual(KeyValues.ACCEPT_MODE_P, orderItem.getAcceptMode())) {
            return flow.getTacheId();
        }

        // 非服务订单不处理
        if (!KiteStringUtils.isEqual(OrderFlowConsts.FLOW_INST_TYPE_ORDITEM, flow.getInstType())) {
            return flow.getTacheId();
        }

        // 写竣工队列表
        writeWqFinish(flow, orderItem);

        // 更新订单项为竣工状态
        updateOrderItemFinish(flow);

        return flow.getTacheId();
    }

    @Override
    protected void after(Flow flow) {
        // 实现空方法。
    }

    /**
     * Description: <br>
     * 获取订单项对象
     *
     * @param flow
     * @return <br>
     */
    private OrderItem getOrderItem(Flow flow) {
        OrderItemMapper orderItemMapper = SpringUtil.getBean(OrderItemMapper.class);
        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("orderItemId", flow.getInstId()).andEqualTo("belongCustId",
            flow.getCustId());
        OrderItem orderItem = orderItemMapper.selectOneByExample(example);
        return orderItem;
    }

    /**
     * Description: <br>
     * 写竣工队列表
     *
     * @taskId <br>
     * @param flow <br>
     */
    private void writeWqFinish(Flow flow, OrderItem orderItem) {
        // 判定竣工队列表是否已经存在
        WqFinishMapper wqFinishMapper = SpringUtil.getBean(WqFinishMapper.class);
        Example example = new Example(WqFinish.class);
        example.createCriteria().andEqualTo("orderItemId", flow.getInstId()).andEqualTo("custId", flow.getCustId());
        WqFinish wqFinish = wqFinishMapper.selectOneByExample(example);
        if (Objects.nonNull(wqFinish)) {
            return;
        }

        // 新增一条竣工队列数据
        WqFinish insertWqFinish = generateWqFinish(flow, orderItem);
        wqFinishMapper.insert(insertWqFinish);
    }

    /**
     * Description: <br>
     * 生成一条竣工队列数据
     *
     * @taskId <br>
     * @param flow
     * @return <br>
     */
    private WqFinish generateWqFinish(Flow flow, OrderItem orderItem) {
        WqFinish wqFinish = new WqFinish();
        wqFinish.setTacheId(flow.getTacheId());
        wqFinish.setOrderItemId(flow.getInstId());

        wqFinish.setServiceOfferId(orderItem.getServiceOfferId());
        wqFinish.setCustId(flow.getCustId());
        wqFinish.setCompInstId(orderItem.getObjId());
        wqFinish.setDealGrade("-1");
        wqFinish.setFailureCode("0");
        wqFinish.setFailureNum("0");
        wqFinish.setFailureDesc("");
        wqFinish.setFailureJavaStack("");
        wqFinish.setFinishFlag("0");
        wqFinish.setItime(KiteDateUtils.getCurrentDate());
        wqFinish.setLanId(orderItem.getAcceptLanId());
        wqFinish.setOfferKind(orderItem.getOfferKind());
        wqFinish.setPrewarnFlag(OrderFlowConsts.WQ_PREWARN_FLAG_NORMAL);

        // 获取产品实例对象
        ProdInst prodInst = FlowEngine.getProdInfo(orderItem);
        if (Objects.nonNull(prodInst)) {
            wqFinish.setProductId(prodInst.getProdId());
            wqFinish.setRegionId(prodInst.getRegionId());
        }

        return wqFinish;
    }

    /**
     * Description: <br>
     * 更新为竣工状态
     * 
     * @taskId <br>
     * @param flow
     * @param orderItem <br>
     */
    private void updateOrderItemFinish(Flow flow) {
        OrderItemMapper orderItemMapper = SpringUtil.getBean(OrderItemMapper.class);
        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("orderItemId", flow.getInstId()).andEqualTo("belongCustId",
            flow.getCustId());

        OrderItem updOrderItem = new OrderItem();
        updOrderItem.setStatusCd(CoreDataInteract.EVT_0001_301100);
        updOrderItem.setStatusDate(LocalDateTime.now());
        orderItemMapper.updateByExampleSelective(updOrderItem, example);
    }

}
